package com.csdn.springbootmybatis.controller;

import com.csdn.springbootmybatis.mode.Account;
import com.csdn.springbootmybatis.mode.Permission;
import com.csdn.springbootmybatis.mode.Role;
import com.csdn.springbootmybatis.service.impl.AccountService;
import com.csdn.springbootmybatis.service.impl.PermissionService;
import com.csdn.springbootmybatis.service.impl.RoleService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *      用户账户相关
 * @Date: 2019/8/24 15:12
 */
@Controller
@RequestMapping("/manager")
public class ManagerController {

    @Autowired
    private AccountService accountService;
    @Autowired
    PermissionService permissionSrv;
    @Autowired
    private RoleService roleService;

    /**
     * 账户
     */
    @RequestMapping("accountList")
    public  String accountList(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5" ) int pageSize, Model model) {
        PageInfo<Account> page = accountService.findByPage(pageNum,pageSize);
        model.addAttribute("page", page);
        return "manager/accountList";
    }

    /**
     * 权限
     */
    @RequestMapping("permissionList")
    public  String permissionList(@RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "5" ) int pageSize,Model model) {
        PageInfo<Permission> page = permissionSrv.findByPage(pageNum,pageSize);
        model.addAttribute("page", page);
        return "manager/permissionList";
    }

    /**
     * 弹出修改页面
     */
    @RequestMapping("permissionModify")
    public  String permissionModify(@RequestParam int id,Model model) {

        Permission permission = permissionSrv.findById(id);

        model.addAttribute("p", permission);
        return "manager/permissionModify";
    }

    /**
     * 弹出添加页面，和修改可以复用
     */
    @RequestMapping("permissionAdd")
    public  String permissionAdd(Model model) {
        return "manager/permissionModify";
    }


    @RequestMapping("roleList")
    public  String roleList(@RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "5" ) int pageSize,Model model) {
        PageInfo<Role> page = roleService.findByPage(pageNum,pageSize);
        model.addAttribute("page", page);
        return "manager/roleList";
    }


    /**
     * 给角色 添加/ 修改权限，重要的方法
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("rolePermission/{id}")
    public  String rolePermission(@PathVariable int id, Model model, HttpServletRequest request) {

        Role role = roleService.findById(id);
        System.out.println("role:" + ToStringBuilder.reflectionToString(role));
        //先把所有的权限展示出来，让角色去选（前端用each去展示）
        List<Permission> pList =  permissionSrv.findAll();
        model.addAttribute("pList", pList);
        model.addAttribute("role", role);
        ///???
//        Object attribute = request.getSession().getAttribute("account");
//        System.out.println("attribute:" + ToStringBuilder.reflectionToString(attribute));
        return "manager/rolePermission";
    }


}
